#include "stdafx.h"
#include "net/DialogSocket.hpp"
#include "charset/ascii.hpp"
using JHCPP::charset::CAscii;
#include <cstring>

NET_NAMESPACE_BEGIN

CDialogSocket::CDialogSocket() : m_pBuffer(0), m_pNext(0), m_pEnd(0)
{
	allocBuffer();
}

CDialogSocket::CDialogSocket(const CSocketAddress& address)
	: CStreamSocket(address),
	m_pBuffer(0),
	m_pNext(0),
	m_pEnd(0)
{
	allocBuffer();
}

CDialogSocket::CDialogSocket(const CSocket& socket)
	: CStreamSocket(socket),
	m_pBuffer(0),
	m_pNext(0),
	m_pEnd(0)
{
	allocBuffer();
}

CDialogSocket::CDialogSocket(const CDialogSocket& socket)
	: CStreamSocket(socket),
	m_pBuffer(0),
	m_pNext(0),
	m_pEnd(0)
{
	allocBuffer();
}


CDialogSocket::~CDialogSocket()
{
	delete [] m_pBuffer;
}

CDialogSocket& CDialogSocket::operator = (const CSocket& socket)
{
	CStreamSocket::operator = (socket);
	m_pNext   = m_pBuffer;
	m_pEnd    = m_pBuffer;
	return *this;
}

CDialogSocket& CDialogSocket::operator = (const CDialogSocket& socket)
{
	CStreamSocket::operator = (socket);
	m_pNext   = m_pBuffer;
	m_pEnd    = m_pBuffer;
	return *this;
}

void CDialogSocket::sendByte(unsigned char ch)
{
	sendBytes(&ch, 1);
}

void CDialogSocket::sendString(const char* str)
{
	sendBytes(str, (int) std::strlen(str));
}

void CDialogSocket::sendString(const std::string& str)
{
	sendBytes(str.data(), (int) str.length());
}

void CDialogSocket::sendMessage(const std::string& message)
{
	std::string line;
	line.reserve(message.length() + 2);
	line.append(message);
	line.append("\r\n");
	sendString(line);
}

void CDialogSocket::sendMessage(const std::string& message, const std::string& arg)
{
	std::string line;
	line.reserve(message.length() + arg.length() + 3);
	line.append(message);
	if (!arg.empty())
	{
		line.append(" ");
		line.append(arg);
	}
	line.append("\r\n");
	sendString(line);
}

void CDialogSocket::sendMessage(const std::string& message, const std::string& arg1, const std::string& arg2)
{
	std::string line;
	line.reserve(message.length() + arg1.length() +arg2.length() + 4);
	line.append(message);
	line.append(" ");
	line.append(arg1);
	if (!arg2.empty())
	{
		line.append(" ");
		line.append(arg2);
	}
	line.append("\r\n");
	sendString(line);
}

bool CDialogSocket::receiveMessage(std::string& message)
{
	message.clear();
	return receiveLine(message);
}

int CDialogSocket::receiveStatusMessage(std::string& message)
{
	message.clear();
	int status = receiveStatusLine(message);
	if (status < 0)
	{
		while (status <= 0)
		{
			message += '\n';
			status = receiveStatusLine(message);
		}
	}
	return status;
}

int CDialogSocket::get()
{
	refill();
	if (m_pNext != m_pEnd)
		return std::char_traits<char>::to_int_type(*m_pNext++);
	else
		return EOF_CHAR;
}

int CDialogSocket::peek()
{
	refill();
	if (m_pNext != m_pEnd)
		return std::char_traits<char>::to_int_type(*m_pNext);
	else
		return EOF_CHAR;
}

void CDialogSocket::synch()
{
	sendUrgent(TELNET_DM);
}

void CDialogSocket::sendTelnetCommand(unsigned char command)
{
	unsigned char buffer[2];
	buffer[0] = TELNET_IAC;
	buffer[1] = command;
	sendBytes(buffer, 2);
}

void CDialogSocket::sendTelnetCommand(unsigned char command, unsigned char arg)
{
	unsigned char buffer[3];
	buffer[0] = TELNET_IAC;
	buffer[1] = command;
	buffer[2] = arg;
	sendBytes(buffer, 3);
}

void CDialogSocket::refill()
{
	if (m_pNext == m_pEnd)
	{
		int n = receiveBytes(m_pBuffer, RECEIVE_BUFFER_SIZE);
		if (n > 0)
		{
			m_pNext = m_pBuffer;
			m_pEnd  = m_pBuffer + n;
		}
	}
}

void CDialogSocket::allocBuffer()
{
	m_pBuffer = new char [RECEIVE_BUFFER_SIZE];
	m_pNext   = m_pBuffer;
	m_pEnd    = m_pBuffer;
}

bool CDialogSocket::receiveLine(std::string& line)
{
	// An old wisdom goes: be strict in what you emit
	// and generous in what you accept.
	int ch = get();
	while (ch != EOF_CHAR && ch != '\r' && ch != '\n')
	{
		line += (char) ch;
		ch = get();
	}
	if (ch == '\r' && peek() == '\n')
		get();
	else if (ch == EOF_CHAR)
		return false;
	return true;
}

int CDialogSocket::receiveStatusLine(std::string& line)
{
	int status = 0;
	int ch = get();
	if (ch != EOF_CHAR) line += (char) ch;
	int n = 0;
	while (CAscii::isDigit(ch) && n < 3)
	{
		status *= 10;
		status += ch - '0';
		++n;
		ch = get();
		if (ch != EOF_CHAR) line += (char) ch;
	}
	if (n == 3)
	{
		if (ch == '-')
			status = -status;
	}
	else status = 0;
	if (ch != EOF_CHAR) receiveLine(line);
	return status;
}

int CDialogSocket::receiveRawBytes(void* buffer, int length)
{
	refill();
	int n = static_cast<int>(m_pEnd - m_pNext);
	if (n > length) n = length;
	std::memcpy(buffer, m_pNext, n);
	m_pNext += n;
	return n;
}

NET_NAMESPACE_END
